"""
Created on 18-Dec-2009

@author: dexen
"""

import time
import threading 

RUNNING = 1
STOPPED = 2
PAUSED = 3

# TODO: ensure the release of locks during failure.
class MasterComm(object):    
    def __init__(self):
        self.slave_names = []
        self.lock = threading.Lock()
        self.state = RUNNING
         
    def add_slaves(self, slave_names):
        with self.lock:
            self.slave_names.extend(slave_names)
        
    def get_slaves(self):
        self.lock.acquire()
        res = self.slave_names
        self.slave_names = []
        self.lock.release()
        return res

    def get_state(self):
        with self.lock:
            return self.state
    
    def set_state(self, state):
        with self.lock:
            self.state = state

    def get_info(self):
        return self.master_info, self.slaves_info
    
    def set_info(self, master_info, slaves_info):
        self.master_info = master_info
        self.slaves_info = slaves_info

    def is_running(self):
        return self.get_state() == RUNNING

